package com.dada.tiku.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.dada.tiku.dao.mapper.PaperSubjectMapper;
import com.dada.tiku.dao.mapper.SubjectAnswerMapper;
import com.dada.tiku.dao.mapper.SubjectOptionsMapper;
import com.dada.tiku.dao.mapper.SubjectUserMapper;
import com.dada.tiku.dao.mapper.TSubTypeMapper;
import com.dada.tiku.dao.mapper.TSubjectMapper;
import com.dada.tiku.dao.mapper.TUserMapper;
import com.dada.tiku.pojo.PaperSubject;
import com.dada.tiku.pojo.PaperSubjectExample;
import com.dada.tiku.pojo.Subject;
import com.dada.tiku.pojo.SubjectAnswer;
import com.dada.tiku.pojo.SubjectAnswerExample;
import com.dada.tiku.pojo.SubjectOptions;
import com.dada.tiku.pojo.SubjectOptionsExample;
import com.dada.tiku.pojo.SubjectOptionsExample.Criteria;
import com.dada.tiku.pojo.SubjectUser;
import com.dada.tiku.pojo.SubjectUserExample;
import com.dada.tiku.pojo.TSubType;
import com.dada.tiku.pojo.TSubTypeExample;
import com.dada.tiku.pojo.TSubject;
import com.dada.tiku.pojo.TSubjectExample;
import com.dada.tiku.pojo.TUser;
import com.dada.tiku.pojo.TUserExample;
import com.dada.tiku.service.SubjectService;
import com.dada.tiku.utils.WxResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class SubjectServiceImpl implements SubjectService {

	@Autowired
	private TSubjectMapper subjectMapper;

	@Autowired
	private SubjectOptionsMapper optionsMapper;

	/*
	 * 查找所有的题目
	 * 
	 * @see com.dada.tiku.service.SubjectService#queryAll(java.lang.Integer,
	 * java.lang.Integer)
	 */
	public WxResult queryAll(Integer start, Integer count, String openid) {
		//根据openid获取用户id方法
		Integer userid = getUserid(openid);
		// 先开启分页查找题目
		PageHelper.startPage(start, count);
		TSubjectExample example = new TSubjectExample();
		example.setOrderByClause("id");
		// 这里查出了分页的题目集合
		List<TSubject> subjectsList = subjectMapper.selectByExample(example);
		List<Subject> resultList = new ArrayList<Subject>();
		// 遍历集合，根据题目找选项然后封装到Subject对象中
		for (TSubject tSubject : subjectsList) {
			SubjectOptionsExample exam = new SubjectOptionsExample();
			exam.setOrderByClause("id");
			Criteria criteria = exam.createCriteria();
			criteria.andSubjectidEqualTo(tSubject.getId());
			List<SubjectOptions> optionsList = optionsMapper.selectByExample(exam);
			// 创建Subject对象，封装题目对象
			Subject sub = new Subject();
			sub.setTsubjec(tSubject);
			// 遍历集合选项集合封装选项
			for (SubjectOptions subjectOptions : optionsList) {
				sub.getOptions().add(subjectOptions);
			}
			
			SubjectUserExample subexample = new SubjectUserExample();
			com.dada.tiku.pojo.SubjectUserExample.Criteria createCriteria = subexample.createCriteria();
			createCriteria.andUseridEqualTo(userid);
			createCriteria.andSubjectidEqualTo(tSubject.getId());
			//封装用户对该词汇是否收藏
			List<SubjectUser> subjectUsers = subjectUserMapper.selectByExample(subexample);
			if(subjectUsers ==null ||subjectUsers.isEmpty() == true||subjectUsers.get(0).getCollection()==null){
				sub.setConnection(false);
			}else{
				sub.setConnection(Integer.parseInt(subjectUsers.get(0).getCollection())==1);
			}
			// 走到这里封装好了Subject对象需要添加到结果集合中
			resultList.add(sub);
		}
		PageInfo<Subject> info = new PageInfo<>(resultList);
//		System.out.println(info.getTotal());
		WxResult result = new WxResult();
		result.setData(resultList);
		//将总共查到多少数据封装到这里,但是注意这里是字符串格式类型
		result.setMsg(info.getTotal()+"");

		return result;
	}
	
	@Autowired
	private TSubTypeMapper typeMapper;
	/*
	 * 根据分类查询题目 逻辑和上面差不多，就是需要加上分类
	 * 
	 * @see
	 * com.dada.tiku.service.SubjectService#queryByCategory(java.lang.String,
	 * java.lang.Integer, java.lang.Integer)
	 */
	public List<Subject> queryByCategory(String categoryname, Integer start, Integer count, String openid) {
		//根据openid获取用户id方法
		Integer userid = getUserid(openid);
		
		TSubTypeExample typeExample = new TSubTypeExample();
		com.dada.tiku.pojo.TSubTypeExample.Criteria typeCriteria = typeExample.createCriteria();
		typeCriteria.andTypenameEqualTo(categoryname);
		//先找出分类id，根据分类名称
		List<TSubType> subTypes = typeMapper.selectByExample(typeExample);
		// 先开启分页查找题目
		PageHelper.startPage(start, count);
		TSubjectExample example = new TSubjectExample();
		example.setOrderByClause("id");
		
		//这里需要加上条件，根据分类查
		com.dada.tiku.pojo.TSubjectExample.Criteria criteria = example.createCriteria();
		criteria.andTypeidEqualTo(subTypes.get(0).getId());
		// 这里查出了分页的题目集合
		List<TSubject> subjectsList = subjectMapper.selectByExample(example);
		List<Subject> resultList = new ArrayList<Subject>();
		// 遍历集合，根据题目找选项然后封装到Subject对象中
		for (TSubject tSubject : subjectsList) {
			SubjectOptionsExample exam = new SubjectOptionsExample();
			exam.setOrderByClause("id");
			Criteria c = exam.createCriteria();
			c.andSubjectidEqualTo(tSubject.getId());
			List<SubjectOptions> optionsList = optionsMapper.selectByExample(exam);
			// 创建Subject对象，封装题目对象
			Subject sub = new Subject();
			sub.setTsubjec(tSubject);
			// 遍历集合选项集合封装选项
			for (SubjectOptions subjectOptions : optionsList) {
				sub.getOptions().add(subjectOptions);
			}

			SubjectUserExample subexample = new SubjectUserExample();
			com.dada.tiku.pojo.SubjectUserExample.Criteria createCriteria = subexample.createCriteria();
			createCriteria.andUseridEqualTo(userid);
			createCriteria.andSubjectidEqualTo(tSubject.getId());
			//封装用户对该词汇是否收藏
			List<SubjectUser> subjectUsers = subjectUserMapper.selectByExample(subexample);
			if(subjectUsers ==null ||subjectUsers.isEmpty() == true||subjectUsers.get(0).getCollection()==null){
				sub.setConnection(false);
			}else{
				sub.setConnection(Integer.parseInt(subjectUsers.get(0).getCollection())==1);
			}
			
			// 走到这里封装好了Subject对象需要添加到结果集合中
			resultList.add(sub);
		}
		return resultList;
	}

	/*
	 * 添加错题
	 * 
	 * @see com.dada.tiku.service.SubjectService#addError(java.lang.Integer,
	 * java.lang.Integer)
	 */
	@Autowired
	private SubjectUserMapper subjectUserMapper;
	@Autowired
	private TUserMapper userMapper;
	
	public WxResult addError(Integer subid, String openid) {
		
		TUserExample userExample = new TUserExample();
		com.dada.tiku.pojo.TUserExample.Criteria userCriteria = userExample.createCriteria();
		userCriteria.andOpenidEqualTo(openid);
		//根据openid查找用户
		List<TUser> users = userMapper.selectByExample(userExample);
		Integer userid = users.get(0).getId();
		
		SubjectUserExample example = new SubjectUserExample();
		com.dada.tiku.pojo.SubjectUserExample.Criteria criteria = example.createCriteria();
		criteria.andSubjectidEqualTo(subid);
		criteria.andUseridEqualTo(userid);
		//根据subid和userid找到用户题目表，没有的话就创建，有的话的就该属性
		List<SubjectUser> subjectUsers = subjectUserMapper.selectByExample(example);
		//判断是否存在
		if(subjectUsers ==null || subjectUsers.isEmpty() == true){
			//走到这里代表不存在，需要创建
			SubjectUser subjectUser = new SubjectUser();
			subjectUser.setSubjectid(subid);
			subjectUser.setUserid(userid);
			//添加错题设置isRight为0 ---id 是自增长的不需要设置 --设计有问题没设计status字段
			subjectUser.setIsright("0");
			subjectUserMapper.insert(subjectUser);
		}else{
			//代表数据库中存在该条记录-修改其状态
			SubjectUser subjectUser = subjectUsers.get(0);
			subjectUser.setIsright("0");
			subjectUserMapper.updateByPrimaryKey(subjectUser);
		}
		return WxResult.ok();
	}

	/*
	 * 移除错题
	 * 既然移除错题name一定已经存在过了，不需要再创建 
	 * @see com.dada.tiku.service.SubjectService#addError(java.lang.Integer,
	 * java.lang.Integer)
	 */
	public WxResult removeError(Integer subid, String openid) {
		
		TUserExample userExample = new TUserExample();
		com.dada.tiku.pojo.TUserExample.Criteria userCriteria = userExample.createCriteria();
		userCriteria.andOpenidEqualTo(openid);
		//根据openid查找用户
		List<TUser> users = userMapper.selectByExample(userExample);
		Integer userid = users.get(0).getId();
		
		SubjectUserExample example = new SubjectUserExample();
		com.dada.tiku.pojo.SubjectUserExample.Criteria criteria = example.createCriteria();
		criteria.andSubjectidEqualTo(subid);
		criteria.andUseridEqualTo(userid);
		//根据subid和userid找到用户题目表
		List<SubjectUser> subjectUsers = subjectUserMapper.selectByExample(example);
		//将isRight设置正确
		SubjectUser subjectUser = subjectUsers.get(0);
		subjectUser.setIsright("1");
		subjectUserMapper.updateByPrimaryKey(subjectUser);
		return WxResult.ok();
	}

	/*
	 * 添加收藏
	 * 
	 * @see com.dada.tiku.service.SubjectService#addError(java.lang.Integer,
	 * java.lang.Integer)
	 */
	public WxResult addConnection(Integer subid, String openid) {
		//调用封装的方法获取用户id
		Integer userid = getUserid(openid);
		
		SubjectUserExample example = new SubjectUserExample();
		com.dada.tiku.pojo.SubjectUserExample.Criteria criteria = example.createCriteria();
		criteria.andSubjectidEqualTo(subid);
		criteria.andUseridEqualTo(userid);
		//根据subid和userid找到用户题目表，没有的话就创建，有的话的就该属性
		List<SubjectUser> subjectUsers = subjectUserMapper.selectByExample(example);
		//判断是否存在
		if(subjectUsers ==null || subjectUsers.isEmpty() == true){
			//走到这里代表不存在，需要创建
			SubjectUser subjectUser = new SubjectUser();
			subjectUser.setSubjectid(subid);
			subjectUser.setUserid(userid);
			//添加connection为1收藏 ---id 是自增长的不需要设置 --设计有问题没设计status字段
			subjectUser.setCollection("1");
			subjectUserMapper.insert(subjectUser);
		}else{
			//代表数据库中存在该条记录-修改其状态
			SubjectUser subjectUser = subjectUsers.get(0);
			subjectUser.setCollection("1");
			subjectUserMapper.updateByPrimaryKey(subjectUser);
		}
		return WxResult.ok();
	}

	/*
	 * 取消收藏
	 * 
	 * @see com.dada.tiku.service.SubjectService#addError(java.lang.Integer,
	 * java.lang.Integer)
	 */
	public WxResult cancelConnection(Integer subid, String openid) {
		//调用封装的方法获取用户id
		Integer userid = getUserid(openid);
		
		SubjectUserExample example = new SubjectUserExample();
		com.dada.tiku.pojo.SubjectUserExample.Criteria criteria = example.createCriteria();
		criteria.andSubjectidEqualTo(subid);
		criteria.andUseridEqualTo(userid);
		//根据subid和userid找到用户题目表
		List<SubjectUser> subjectUsers = subjectUserMapper.selectByExample(example);
		//将collection设置正确
		SubjectUser subjectUser = subjectUsers.get(0);
		subjectUser.setCollection("0");
		subjectUserMapper.updateByPrimaryKey(subjectUser);
		return WxResult.ok();
	}
	
	
	/*
	 * 查找答案
	 * 
	 * @see com.dada.tiku.service.SubjectService#addError(java.lang.Integer,
	 * java.lang.Integer)
	 */
	@Autowired
	private SubjectAnswerMapper answerMapper;
	@Autowired
	private SubjectOptionsMapper subjectOptionsMapper;
	public List<SubjectOptions> findAnswer(Integer subid) {
		SubjectAnswerExample example = new SubjectAnswerExample();
		com.dada.tiku.pojo.SubjectAnswerExample.Criteria criteria = example.createCriteria();
		criteria.andSubjectidEqualTo(subid);
		List<SubjectAnswer> answers = answerMapper.selectByExample(example);
		//可能有多选直接返回
		List<SubjectOptions> list = new ArrayList<SubjectOptions>();
		for (SubjectAnswer subjectAnswer : answers) {
			SubjectOptions subjectOptions = subjectOptionsMapper.selectByPrimaryKey(subjectAnswer.getOptionid());
			list.add(subjectOptions);
		}
		return list;
	}
	
	/**
	 * 根据试卷id查找对应的试题集合
	 */
	@Autowired
	private PaperSubjectMapper paperSubjectMapper;
	
	public WxResult findPaperSubjectList(Integer paperid, Integer start, Integer count, String openid) {
		//调用封装的方法获取用户id
		Integer userid = getUserid(openid);
		
		PaperSubjectExample example = new PaperSubjectExample();
		example.setOrderByClause("id");
		com.dada.tiku.pojo.PaperSubjectExample.Criteria PScriteria = example.createCriteria();
		PScriteria.andPaperidEqualTo(paperid);
		
		PageHelper.startPage(start,count);
		
		List<PaperSubject> paperSubjectList = paperSubjectMapper.selectByExample(example);
		//在外层封装一个结果集对象
		List<Subject> subjectList  = new ArrayList<Subject>();
		for (PaperSubject paperSubject : paperSubjectList) {
			//遍历这个集合能找到所有的试题id,在根据试题id封装试题集合,
			TSubject tSubject = subjectMapper.selectByPrimaryKey(paperSubject.getSubjectid());
			//创建subject对象
			Subject subject = new Subject();
			subject.setTsubjec(tSubject);
			
			//查找该题目下的选项集合
			SubjectOptionsExample exam = new SubjectOptionsExample();
			exam.setOrderByClause("id");
			Criteria c = exam.createCriteria();
			c.andSubjectidEqualTo(tSubject.getId().intValue());
			List<SubjectOptions> optionsList = optionsMapper.selectByExample(exam);
			// 遍历集合选项集合封装选项
			for (SubjectOptions subjectOptions : optionsList) {
				subject.getOptions().add(subjectOptions);
			}
			
			SubjectUserExample subexample = new SubjectUserExample();
			com.dada.tiku.pojo.SubjectUserExample.Criteria createCriteria = subexample.createCriteria();
			createCriteria.andUseridEqualTo(userid);
			createCriteria.andSubjectidEqualTo(tSubject.getId());
			//封装用户对该词汇是否收藏
			List<SubjectUser> subjectUsers = subjectUserMapper.selectByExample(subexample);
			if(subjectUsers ==null ||subjectUsers.isEmpty() == true||subjectUsers.get(0).getCollection()==null){
				subject.setConnection(false);
			}else{
				subject.setConnection(Integer.parseInt(subjectUsers.get(0).getCollection())==1);
			}
			
			//将subject对象封装到subjectList结果集合中
			subjectList.add(subject);
		}
		PageInfo<Subject> info = new PageInfo<>(subjectList);
//		System.out.println(info.getTotal());
		WxResult result = new WxResult();
		result.setData(subjectList);
		//将总共查到总条数封装到msg中,但是这里是字符串格式
		result.setMsg(info.getTotal()+"");
		
		return result;
	}

	/*
	 * 判断用户试题是否收藏
	 * @see com.dada.tiku.service.SubjectService#isCollection(java.lang.Integer, java.lang.String)
	 */
	public WxResult isCollection(Integer id, String openid) {
		//调用封装的方法获取用户id
		Integer userid = getUserid(openid);
		
		SubjectUserExample example = new SubjectUserExample();
		com.dada.tiku.pojo.SubjectUserExample.Criteria criteria = example.createCriteria();
		criteria.andUseridEqualTo(userid);
		criteria.andSubjectidEqualTo(id);
		List<SubjectUser> subjectUsers = subjectUserMapper.selectByExample(example);
		//这里有数据的话就是一条数据,否则没有
		if(subjectUsers ==null||subjectUsers.isEmpty()||Integer.parseInt(subjectUsers.get(0).getCollection()) ==0){
			//这里代表用户没有收藏该试题
			return WxResult.build(300, "用户没有收藏该试题");
		}
		
		return WxResult.build(200, "用户收藏了该试题");
	}

	/*
	 * 获取用户所有收藏试题集合
	 * @see com.dada.tiku.service.SubjectService#getCollectionList(java.lang.Integer, java.lang.String)
	 */
	public WxResult getCollectionList(String openid,Integer start, Integer count) {
		//调用封装的方法获取用户id
		Integer userid = getUserid(openid);
		//开启分页
		PageHelper.startPage(start, count);
		
		SubjectUserExample example = new SubjectUserExample();
		com.dada.tiku.pojo.SubjectUserExample.Criteria criteria = example.createCriteria();
		criteria.andUseridEqualTo(userid);
		criteria.andCollectionEqualTo("1");
		List<SubjectUser> subjectUsers = subjectUserMapper.selectByExample(example);
		//封装一个试题集合对象
		List<Subject> list = new ArrayList<Subject>();
		//根据试题用户集合遍历,根据试题id封装试题对象和选项对象
		for (SubjectUser subjectUser : subjectUsers) {
			//创建Subject对象
			Subject subject = new Subject();
			//根据subjectUser中的subjectid查找试题对象封装到subject中
			TSubject tSubject = subjectMapper.selectByPrimaryKey(subjectUser.getSubjectid());
			
			subject.setTsubjec(tSubject);
			
			//根据试题对象id查找所有的选项集合
			SubjectOptionsExample exam = new SubjectOptionsExample();
			exam.setOrderByClause("id");
			Criteria examcriteria = exam.createCriteria();
			examcriteria.andSubjectidEqualTo(tSubject.getId());
			List<SubjectOptions> optionsList = optionsMapper.selectByExample(exam);
			
			// 遍历集合选项集合封装选项
			for (SubjectOptions subjectOptions : optionsList) {
				subject.getOptions().add(subjectOptions);
			}
			
			//设置subject对象connection为true
			subject.setConnection(true);
			
			// 走到这里封装好了Subject对象需要添加到结果集合中
			list.add(subject);
		}
		
		//取出最大数量
		PageInfo<SubjectUser> info = new PageInfo<>(subjectUsers);
//		System.out.println(info.getTotal());
		WxResult result = new WxResult();
		result.setData(list);
		//将总共查到多少数据封装到这里,但是注意这里是字符串格式类型
		result.setMsg(info.getTotal()+"");
		
		return result;
	}

	/*
	 * 获取用户所有错题集合
	 * @see com.dada.tiku.service.SubjectService#getErrorList(java.lang.Integer, java.lang.String)
	 */
	public WxResult getErrorList(String openid,Integer start, Integer count) {
		//调用封装的方法获取用户id
		Integer userid = getUserid(openid);
		//开启分页
		PageHelper.startPage(start, count);
		
		SubjectUserExample example = new SubjectUserExample();
		com.dada.tiku.pojo.SubjectUserExample.Criteria criteria = example.createCriteria();
		criteria.andUseridEqualTo(userid);
		criteria.andIsrightEqualTo("0");
		List<SubjectUser> subjectUsers = subjectUserMapper.selectByExample(example);
		//封装一个试题集合对象
		List<Subject> list = new ArrayList<Subject>();
		//根据试题用户集合遍历,根据试题id封装试题对象和选项对象
		for (SubjectUser subjectUser : subjectUsers) {
			//创建Subject对象
			Subject subject = new Subject();
			//根据subjectUser中的subjectid查找试题对象封装到subject中
			TSubject tSubject = subjectMapper.selectByPrimaryKey(subjectUser.getSubjectid());
			
			subject.setTsubjec(tSubject);
			
			//根据试题对象id查找所有的选项集合
			SubjectOptionsExample exam = new SubjectOptionsExample();
			exam.setOrderByClause("id");
			Criteria examcriteria = exam.createCriteria();
			examcriteria.andSubjectidEqualTo(tSubject.getId());
			List<SubjectOptions> optionsList = optionsMapper.selectByExample(exam);
			
			// 遍历集合选项集合封装选项
			for (SubjectOptions subjectOptions : optionsList) {
				subject.getOptions().add(subjectOptions);
			}
			
			//封装用户对该词汇是否收藏
			subject.setConnection(subjectUser.getCollection()=="1");
			
			// 走到这里封装好了Subject对象需要添加到结果集合中
			list.add(subject);
		}
		
		//取出最大数量
		PageInfo<SubjectUser> info = new PageInfo<>(subjectUsers);
//		System.out.println(info.getTotal());
		WxResult result = new WxResult();
		result.setData(list);
		//将总共查到多少数据封装到这里,但是注意这里是字符串格式类型
		result.setMsg(info.getTotal()+"");
		return result;
	}
	
	public Integer getUserid(String openid){
		TUserExample userExample = new TUserExample();
		com.dada.tiku.pojo.TUserExample.Criteria userCriteria = userExample.createCriteria();
		userCriteria.andOpenidEqualTo(openid);
		//根据openid查找用户
		List<TUser> users = userMapper.selectByExample(userExample);
		Integer userid = users.get(0).getId();
		return userid;
	}
}
